let CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const gwviewer = "node_modules/gw-map3d/GWViewer";
const CompressionPlugin = require("compression-webpack-plugin");
// 头部引入
const isProduction = process.env.NODE_ENV === 'production'
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
const name ='轨道预测' // page title
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  lintOnSave: false,
  transpileDependencies: true,
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  devServer: {
    port: 8099,
    hot:true,//自动保存
    open : true,//自动启动
    // disableHostCheck: true,
    // 需要内网的其它机器也能访问时，将值改成 '0.0.0.0'
    host: "0.0.0.0",
    proxy: {
      "baseServer":{
        target: "http://172.16.15.15:9003/",
        secure: false, // false为http访问，true为https访问
        changeOrigin: true, // 跨域访问设置，true代表跨域
        ws: true,
        pathRewrite: { "^/baseServer": "" }
      },
      "configServer":{
        target: "http://172.16.15.15:9501/",
        secure: false, // false为http访问，true为https访问
        changeOrigin: true, // 跨域访问设置，true代表跨域
        ws: true,
        pathRewrite: { "^/configServer": "" }
      },
      "regionServer":{
        target: "http://172.16.15.15:9003/rsmis-region-service/",
        secure: false, // false为http访问，true为https访问
        changeOrigin: true, // 跨域访问设置，true代表跨域
        ws: true,
        pathRewrite: { "^/regionServer": "" }
      },
    }
  },
  // 链式webpack
  chainWebpack(config) {

    config
      .when(isProduction,
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
            chunks: 'all',
            maxInitialRequests: 5,
            cacheGroups: {
              libs: {
                name: 'chunk-libs',
                test: /[\\/]node_modules[\\/]/,
                priority: 10,
                chunks: 'initial'
              },
              elementUI: {
                name: 'chunk-elementUI',
                priority: 20,
                test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
                enforce: true,
                reuseExistingChunk: true
              },
              echarts: {
                name: 'chunk-echarts',
                priority: 20,
                test: /[\\/]node_modules[\\/]_?echarts(.*)/,
                enforce: true,
                reuseExistingChunk: true
              },
              commons: {
                name: 'chunk-commons',
                test: resolve('src/components'),
                minChunks: 3,
                priority: 5,
                reuseExistingChunk: true
              }
            }
          })
          config.optimization.runtimeChunk('single')
        }
      )
  },
  configureWebpack: config => {
    const plugins = [
      new CopyWebpackPlugin([
        {
          from: path.resolve(__dirname, gwviewer),
          to: "GWViewer"
        }
      ]),
      new NodePolyfillPlugin()
    ];
    if (isProduction) {
      plugins.push(new CompressionPlugin({
        filename: "[path].gz[query]", // 提示compression-webpack-plugin@3.0.0的话asset改为filename
        algorithm: "gzip",
        test: /\.js$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0.8,
        deleteOriginalAssets: false,
      }));
    }
    return {
      plugins: plugins,
      name: name,
      resolve: {
        alias: {
          "@": resolve("src")
        }
      },
      devtool: isProduction ? false : "eval-source-map"
    };
  }
};
